package org.myblog.myblogserver.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.myblog.myblogserver.code.entity.Article;
import org.myblog.myblogserver.code.service.ArticleService;
import org.myblog.myblogserver.code.service.TabsService;
import org.myblog.myblogserver.code.service.UserService;
import org.myblog.myblogserver.dao.ArticleDao;
import org.myblog.myblogserver.dao.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "Article")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService  userService;

    @Autowired
    private TabsService  tabsService;

    private Result result=new Result();


    /**
     * post  /article/insert<br/>
     * json<br/>
     * time和id和views为null<br/>
     *
     * @author 智哥
     * @param article
     * @return Result
     * */
    @ApiOperation(value = "Article新增")
    @PostMapping("/insert")
    public Result insert(@RequestBody Article article) {
        article.setId(null);
        article.setViews(null);
        String author=article.getAuthor_id();
        Integer tab=article.getTab_id();
        String title=article.getTitle();
        if(userService.getById(author)==null){
            result.client_fail("用户不存在", null);
            return result;
        }
        if(tabsService.getById(tab)==null){
            result.client_fail("标签不存在", null);
            return result;
        }
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("title",title);
        if(articleService.getOne(queryWrapper)!=null){
            result.client_fail("标题已存在", null);
            return result;
        }
        article.setTime(new java.util.Date());
        if(!articleService.save(article)){
            result.client_fail("添加失败", null);
            return result;
        }
        result.success("添加成功", null);
        return result;
    }


    /**
     * post  /article/selecteq<br/>
     * json<br/>
     * 三个条件 标签id 标题 发布时间<br/>
     *
     * @author 智哥
     * @param article
     * @return Result
     * */
    @ApiOperation(value = "Article条件查询")
    @PostMapping("/selecteq")
    public Result selecteq(@RequestBody Article article) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        if(article.getTab_id()!=null) queryWrapper.eq("tab_id", article.getTab_id());
        if(article.getTitle()!=null) queryWrapper.like("title", article.getTitle());
        if(article.getTime()!=null) queryWrapper.eq("time", article.getTime());
        List<Article> list =articleService.list(queryWrapper);
        if(list.isEmpty()){
            result.client_fail("没有数据", null);
        }else{
            List<ArticleDao> li=new ArrayList<>();
            for(Article ar:list){
                ArticleDao articleDao = new ArticleDao();
                articleDao.setId(ar.getId());
                articleDao.setTitle(ar.getTitle());
                articleDao.setAuthor(userService.getById(ar.getAuthor_id()).getUsername());
                articleDao.setTab(tabsService.getById(ar.getTab_id()).getTab_name());
                articleDao.setContent(ar.getContent());
                articleDao.setUrl(ar.getUrl());
                articleDao.setTime(ar.getTime());
                articleDao.setViews(ar.getViews());
                li.add(articleDao);
            }
            result.success("查询成功", li);
        }
        return result;
    }


    /**
     * post  /article/update<br/>
     * json<br/>
     * 以标题查询修改<br/>
     *
     * @author 智哥
     * @param article
     * @return Result
     * */
    @ApiOperation(value = "Article修改")
    @PostMapping("/update")
    public Result update(@RequestBody Article article) {
        if(article.getTitle()==null){
            result.client_fail("文章不存在", null);
            return result;
        }
        article.setTime(new java.util.Date());
        article.setViews(null);
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("title", article.getTitle());
        if(article.getTime()!=null) updateWrapper.set("time", new java.util.Date());
        if(article.getTab_id()!=null) updateWrapper.set("tab_id", article.getTab_id());
        if(article.getUrl()!=null) updateWrapper.set("url", article.getUrl());

        if(articleService.update(updateWrapper)){
            result.success("修改成功", null);
        }else{
            result.client_fail("修改失败", null);
        }
        return result;
    }


    /**
     * post  /article/deleteAlleq<br/>
     * json<br/>
     * 只有一个title条件删除<br/>
     *
     * @author 智哥
     * @param article
     * @return Result
     * */
    @ApiOperation(value = "Article条件删除")
    @PostMapping("/deleteAlleq")
    public Result deleteAll(@RequestBody Article article) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if(article.getTitle()!=null) queryWrapper.eq("title", article.getTitle());
        if(articleService.remove(queryWrapper)){
            result.success("删除成功", true);
        }else {
            result.server_fail("删除失败", false);
        }
        return result;
    }

    /**
     * post  /article/addViews<br/>
     *
     * @author 智哥
     * @param id
     * @return Result
     * */
    @ApiOperation(value = "Article观看数add")
    @PostMapping("/addViews")
    public Result addViews(@RequestParam(value = "id") Integer id) {
        Article article = articleService.getById(id);
        article.setViews(article.getViews()+1);
        if(articleService.updateById(article)){
            result.success("修改成功", null);
        }else {
            result.server_fail("修改失败", null);
        }
        return result;
    }
}